package com.u2b.business.location;

import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import com.u2b.business.sort.SortProxy;
import com.u2b.global.utils.Constants;
import com.u2b.global.utils.LoggerUtils;
import com.u2b.kvdb.KVDBHandler;

public class DistanceService {
	private final BaseLoactionService baseLoactionService;
	private final KVDBHandler kvdbHandler;

	public DistanceService() {
		baseLoactionService = new BaseLoactionService();
		kvdbHandler = new KVDBHandler();
	}

	/*
     * 
     */
	private void addDistance(HashMap<String, Object> value,
			HashMap<String, Object> initParams) {
		double lat1 = Double.valueOf(initParams.get("latitude").toString());
		double lng1 = Double.valueOf(initParams.get("longitude").toString());
		double lng2 = Double.valueOf(value.get("longitude").toString());
		double lat2 = (Double.valueOf(value.get("latitude").toString()));
		double distance = baseLoactionService.getDistance(lat1, lng1, lat2,
				lng2);

		value.put("distance", distance+"km");
	}
   /**
    * 向从客户端得到的数据添加geoHash字段,在上传活动和上传状态的时候需要
    * @param initParams
    * 从客户端获得的参数
    * @return void  
    * 
    */
	public void addGeoHash(HashMap<String, Object> initParams) {

		double lat = Double.valueOf(initParams.get("latitude").toString());
		double lon = Double.valueOf(initParams.get("longitude").toString());
		String geoHash = Geohash.encode(lat, lon);
		initParams.put("geohash", geoHash);

	}

	public void updateDistanceToKVDB(HashMap<String, Object> initParams) {
		String longitude = initParams.get("longitude").toString();
		String latitude = initParams.get("latitude").toString();
		String value = longitude + ":" + latitude;
		String key = initParams.get("owner").toString();
		
		LoggerUtils.log("updateDistanceToKVDB", key, value);
		kvdbHandler.saveToKvdb(key, value);

	}

	// result每次返回的是20条数据,并且要完成排序.不查询KVDB.如状态列表,活动列表等.应该是按时间排序likegeohash
	public void addDistanceAndSortDistance(
			List<HashMap<String, Object>> result,
			HashMap<String, Object> initParams) {
		for (HashMap<String, Object> single : result) {
			addDistance(single, initParams);
		}
		SortProxy.sortList(result, "distance");
	}

	public void addDistance(List<HashMap<String, Object>> result,
			HashMap<String, Object> initParams) {
		for (HashMap<String, Object> single : result) {
			addDistance(single, initParams);
		}
	}

	public void getDistanceFromKVDB(HashMap<String, Object> initValue,
			HashMap<String, Object> result) {
		String uid = initValue.get("uid").toString();
		// 这应该是从KVDB得到值，怎么会是从查询的结果中呢？
		double lat1 = Double.valueOf(initValue.get("latitude").toString());
		double lng1 = Double.valueOf(initValue.get("longitude").toString());
		String location = kvdbHandler.getLocationFromKvdb(uid);
		StringTokenizer stringTokenizer = new StringTokenizer(location, ":");
		double lng2 = Double.valueOf(stringTokenizer.nextToken());
		double lat2 = Double.valueOf(stringTokenizer.nextToken());
		double distance = baseLoactionService.getDistance(lat1, lng1, lat2,
				lng2);
       
		result.put("distance", distance+"km");

	}
}
